


var initTime = 10;
var time = initTime;

var errorCount = 5;
var errorTime = errorCount;

var onlineEl = document.getElementById("online");
var offlineEl = document.getElementById("offline");
var hangupEl = document.getElementById("hangup");
var answerEl = document.getElementById("answer");
var onmuteEl = document.getElementById("onmute");
var offmuteEl = document.getElementById("offmuteEle");

function stateQuery(){
	
	var result = ClientOCX.CCPisOnline();
	i(result);
	result = JSON.parse(result);
	if(result.isOnline){
		errorTime = errorCount;
		i("恢复正常");
	}else{
		errorTime--;
		if(errorTime == 0){
			//超时此次太多，自动离线
			offlineFun();
		}
		e("超时次数：" + errorTime);
	}
	setTimeout(stateQuery,5000);
}

function tike(){
	if(time > 0){
		showMsg(time + "秒，自动准备就绪")
		time--;
		setTimeout(tike,1000);
	}else{
		time = initTime;
		ready();
	}
}

function ready(){
	
	$.post(baseCtiUrl + "operation",{seatId:seatId,operation:"READY"},function(result){
		if(result.resultCode == "SUCC"){

			showMsg("空闲中");
			return;
		}
		showMsg(result.failureMessage);
		
	},"json");
}


function showMsg(msg){

	document.getElementById("cti_state").innerHTML = msg;
	
}

function e(msg){
	if(console && console.error){
		console.error(msg);
	}
}

function i(msg){
	if(console && console.info){
		console.info(msg);
	}
}

function show(El){
	El.style.display = "block";
}

function hide(El){
	El.style.display = "none";
}


function init(){
	//插件初始化成功
	try{
		var result = ClientOCX.CCPinit(); 
		if(result != 0){
			showMsg("插件初始化失败，失败代码：" + result);
			return false;
		}
		onlineEl.style.display = "block";
	
		return true;
	} catch (e) {
		showMsg("您未安装坐席插件");
		return false;	
	}
	
}

function call(phone){
	ClientOCX.CCPmakeCall(0, phone);
	showMsg("正在外呼");
}



function answerFun(){
	ClientOCX.CCPacceptCall(ClientOCX.callid, 0);
}


function hangupFun(){
	
	if(callAlerting){
		//拒接 3：被叫拒绝应答；6：被叫忙；
		ClientOCX.CCPrejectCall(ClientOCX.callid, 3);
	}else{
		//挂机
		ClientOCX.CCPreleaseCall(ClientOCX.callid);
	}
}


//上线
function onlineFun(){
	
	$.post(baseCtiUrl + "agentonwork",{seatId:seatId},function(result){
		if(result.resultCode == "SUCC"){
			var state = ClientOCX.CCPlogin(result.data.restAddr, result.data.restPort, 
			result.data.mainAccount, result.data.mainAccountpwd,
			result.data.subAccount,result.data.subAccountpwd,
		    result.data.voipId, 
		    result.data.voipPwd);
		    //登录成功
		    if(state == 0){
		    	hide(onlineEl);
		    	show(offlineEl);
		    	showMsg("在线");
		    		stateQuery();
		    }else{
		    	showMsg("上线失败，失败代码：" + state);
		    }
		    
			return;
		}
		showMsg(result.failureMessage);
	},"json");
	
}

//离线
function offlineFun(){

	$.post(baseCtiUrl + "operation",{seatId:seatId,operation:"OFFLINE"},function(result){
		if(result.resultCode == "SUCC"){
			ClientOCX.CCPlogout();
			showMsg("离线");
			show(onlineEl);
		    hide(offlineEl);
			return;
		}
		showMsg(result.failureMessage);
	},"json");
	
}

//静音
function onmuteFun(){
	
	$.post(baseCtiUrl + "operation",{seatId:seatId,operation:"ONMUTE",callid:ClientOCX.callid},function(result){
		if(result.resultCode == "SUCC"){
			show(offmuteEl);
			hide(onmuteEl);
			showMsg("静音中");
			return;
		}
		showMsg(result.failureMessage);
	},"json");
	
}

//取消静音
function offmuteFun(){

	$.post(baseCtiUrl + "operation",{seatId:seatId,operation:"OFFMUTE",callid:ClientOCX.callid},function(result){
		if(result.resultCode == "SUCC"){
			hide(offmuteEl);
			show(onmuteEl);
			showMsg("通话中");
			return;
		}
		showMsg(result.failureMessage);
	},"json");
	
}